package amartin47;

import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.LayoutManager;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.*;
import javax.swing.border.EtchedBorder;

public class ChoosableText extends JComponent
{
	private JLabel titleLabel;
	private JLabel lyricsLabel;
	boolean selected = false;
	private PageChooser owner;
	private Slide slide;
	//private static int dimensionSize = 130;
	
	public ChoosableText(Slide slideInfo, PageChooser ownerPage) 
	{
		super();

		this.slide = slideInfo;
		
		this.owner = ownerPage;
		String htmlFormatLyrics = slide.lyrics;
		htmlFormatLyrics = htmlFormatLyrics.replaceAll("\n", " <br> ");
		htmlFormatLyrics = "<html><center>" + htmlFormatLyrics + "</center></html>";
		this.slide.lyrics = htmlFormatLyrics;
		

		//this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		LayoutManager layout;
		layout = new BoxLayout(this, BoxLayout.PAGE_AXIS);
		this.setLayout(layout);

		titleLabel = new JLabel(slide.title);
		lyricsLabel = new JLabel(htmlFormatLyrics);
		Font newLabelFont = new Font(titleLabel.getFont().getName(),Font.BOLD, 14); 
		Font newLyricsFont = new Font(lyricsLabel.getFont().getName(),Font.PLAIN,9); 
		titleLabel.setFont(newLabelFont);
		lyricsLabel.setFont(newLyricsFont);
		titleLabel.setAlignmentX(LEFT_ALIGNMENT);
		lyricsLabel.setAlignmentX(LEFT_ALIGNMENT);
		//lyricsLabel.setHorizontalTextPosition(JLabel.CENTER);
		//lyricsLabel.setVerticalAlignment(SwingConstants.CENTER);
		//lyricsLabel.setMinimumSize(new Dimension(166, 0));
		//lyricsLabel.setPreferredSize(new Dimension(166, 0));

		this.add(titleLabel);
		this.add(lyricsLabel);
		
		this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		
		//this.setPreferredSize(new Dimension(dimensionSize, dimensionSize));
		
		this.addMouseListener(new MouseListener()
				{
					@Override
					public void mouseClicked(MouseEvent arg0) {
						owner.deselectAll();
						owner.setSelectedSlide(ChoosableText.this.slide);
						selected = true;
						repaint();
					}
					public void mouseEntered(MouseEvent arg0) {	}
					public void mouseExited(MouseEvent arg0) { }
					public void mousePressed(MouseEvent arg0) { }
					public void mouseReleased(MouseEvent arg0) { }
				});
	}
	
	protected void paintComponent(Graphics g)
	{
		super.paintComponent(g);
		if(selected)
		{
			g.setColor(Color.YELLOW);
			g.fillRect(0, 0, this.getSize().width, this.getSize().height);
		}
	}

	public void deselect() {
		selected = false;
		this.repaint();
	}
	
	
}
